package com.android.whereismy;

import java.util.Date;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Address;
import android.location.Location;

import com.android.whereismy.util.CmnConsts;
import com.android.whereismy.util.CmnUtils;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * Overlay that indicate current location in address of target
 * @author akarae
 *
 */
public class UnitLocationOverlay extends Overlay {
    
    Location myLocation;
    Address myAddress;
    Date rptTime;
    String freshLevel;
    String provider;
    
    /**
     * MyLocationOverlay constructor.<BR>
     * 
     * @param myLocation
     * @param myAddress
     */
    public UnitLocationOverlay (Location myLocation, Address myAddress, Date rptTime, String provider) {
        this.myLocation = myLocation;
        this.myAddress = myAddress;
        this.freshLevel = CmnUtils.getFreshLevel(rptTime);
        this.rptTime = rptTime;
        this.provider = provider;
    }

    /**
     * override draw.
     * 
     * @param canvas
     * @param mpaView
     * @param shadow
     */
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        
        if (shadow == false) {
            // Convert the location to screen pixels
            Projection projection = mapView.getProjection();
            Point tempPoint = new Point();
            projection.toPixels(mapView.getMapCenter(), tempPoint);
            
            // address info
            Paint addrPaint = new Paint();
            addrPaint.setARGB(150, 0, 0, 0);
            addrPaint.setAntiAlias(true);
            
            RectF addrRectF = new RectF(
                    mapView.getWidth() - mapView.getWidth() + 10, 
                    mapView.getHeight() - mapView.getHeight() + 10, 
                    mapView.getWidth() - 10, 
                    mapView.getHeight() - mapView.getHeight() + 70);
            canvas.drawRoundRect(addrRectF, 5, 5, addrPaint);
            Paint addrTextPaint = new Paint();
            addrTextPaint.setARGB(255, 255, 255, 255);
            addrTextPaint.setTextSize(15);
            if (!CmnUtils.isStrEmpty(myAddress.getAddressLine(0))) {
                canvas.drawText(
                        myAddress.getAddressLine(0), 
                        mapView.getWidth() - mapView.getWidth() + 15, 
                        mapView.getHeight() - mapView.getHeight() + 27, 
                        addrTextPaint);
            }
            if (!CmnUtils.isStrEmpty(myAddress.getAddressLine(1))) {
                canvas.drawText(
                        myAddress.getAddressLine(1),
                        mapView.getWidth() - mapView.getWidth() + 15,
                        mapView.getHeight() - mapView.getHeight() + 45,
                        addrTextPaint);
            }

            if (!CmnUtils.isStrEmpty(freshLevel) && !CmnUtils.isStrEmpty(provider)) {
                    canvas.drawText(
                            "ReportTime : ", 
                            mapView.getWidth() - mapView.getWidth() + 15,
                            mapView.getHeight() - mapView.getHeight() + 63,
                            addrTextPaint);

                if (CmnConsts.FRESH_LEVEL_HIGH.equals(freshLevel)) {
                    addrTextPaint.setARGB(255, 50, 205, 50);
                } else if (CmnConsts.FRESH_LEVEL_MID.equals(freshLevel)) {
                    addrTextPaint.setARGB(255, 0, 0, 205);
                } else if (CmnConsts.FRESH_LEVEL_LOW.equals(freshLevel)) {
                    addrTextPaint.setARGB(255, 255, 0, 0);
                }

                canvas.drawText(
                        rptTime.toLocaleString(), 
                        mapView.getWidth() - mapView.getWidth() + 107,
                        mapView.getHeight() - mapView.getHeight() + 63,
                        addrTextPaint);
                addrTextPaint.setARGB(255, 255, 255, 255);
                canvas.drawText(
                        "Provider : " + provider, 
                        mapView.getWidth() - mapView.getWidth() + 280,
                        mapView.getHeight() - mapView.getHeight() + 63,
                        addrTextPaint);
            }
        }
        
        super.draw(canvas, mapView, shadow);
    }

    @Override
    public boolean onTap(GeoPoint point, MapView mapView) {
    return false;
    }
}
